package gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import database.DBconn;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.Statement;

public class MaskLoginMenu extends JPanel {
	private JTextField usertxtFieldMask;
	private JPasswordField passtxtFieldMask;

	/**
	 * Create the panel.
	 */
	public MaskLoginMenu(JPanel RegisterMenu) {
		RegisterMenu.removeAll();
		RegisterMenu.setLayout(null);
		
		final JPanel panel = new JPanel();
		panel.setBounds(0, 0, 318, 587);
		RegisterMenu.add(panel);
		panel.setLayout(null);
		
		JButton btnRegisterMask = new JButton("Register");
		btnRegisterMask.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				new RegisterMenu(panel).setVisible(true);
				
			}
		});
		btnRegisterMask.setBounds(31, 173, 117, 29);
		panel.add(btnRegisterMask);
		
		JLabel lblCampusInstantMessagingMask = new JLabel("Campus Instant Messaging");
		lblCampusInstantMessagingMask.setBounds(85, 33, 172, 16);
		panel.add(lblCampusInstantMessagingMask);
		
		JLabel lblUsernameMask = new JLabel("Username");
		lblUsernameMask.setBounds(37, 81, 65, 16);
		panel.add(lblUsernameMask);
		
		usertxtFieldMask = new JTextField();
		usertxtFieldMask.setColumns(10);
		usertxtFieldMask.setBounds(139, 75, 134, 28);
		panel.add(usertxtFieldMask);
		
		JLabel lblPasswordMask = new JLabel("Password");
		lblPasswordMask.setBounds(37, 127, 64, 16);
		panel.add(lblPasswordMask);
		
		passtxtFieldMask = new JPasswordField();
		passtxtFieldMask.setBounds(139, 121, 134, 28);
		panel.add(passtxtFieldMask);
		
		JButton logButtonMask = new JButton("Login");
		logButtonMask.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					String sql="SELECT * FROM userinfo";				
					DBconn.getConnection();
			        Statement stmt = DBconn.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
			        ResultSet rset = stmt.executeQuery(sql);						            
		            String user=usertxtFieldMask.getText();
		            String pwd= new String (passtxtFieldMask.getPassword());
		            int tmp=0;
		            while(rset.next()) {
		            String uid=rset.getString("u_id");
		            String uname=rset.getString("u_username");		           
		            String password=rset.getString("u_password");
		            if ((user.equals(uname)) && (pwd.equals(password))){		            	
		            	new ChatMenu(panel, uid).setVisible(true);
		                tmp++;
		            }
		            }
		            if (tmp==0) {
		            JOptionPane.showMessageDialog(null, "Username and Password not in database!");		            
		             }		            
		            
		        } catch (Exception e1) {
		            System.out.print(e1.getMessage());
		        }
				
			}
		});
		logButtonMask.setBounds(158, 173, 117, 29);
		panel.add(logButtonMask);
		
		JLabel label_3 = new JLabel(":");
		label_3.setBounds(95, 69, 9, 41);
		panel.add(label_3);
		
		JLabel label_4 = new JLabel(":");
		label_4.setBounds(95, 108, 9, 54);
		panel.add(label_4);
		
		RegisterMenu.validate();
		RegisterMenu.repaint();

	}

}
